﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLTVNEW
{
    public partial class frmSearch : DevComponents.DotNetBar.Office2007Form
    {
        public frmSearch()
        {
            InitializeComponent();
        }
        QLTVDataContext db = new QLTVDataContext();
        

        void GetAutoComplete(int i)
        {
            AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
            auto.Clear();
            if (i==0)
            {
                var query = from tb in db.THELOAIs select tb.TenTheLoai ;
                if (query !=null)
                {
                    foreach (var item in query)
                    {
                        auto.Add(item.Trim());
                    }
                }
                txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
                txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtSearch.AutoCompleteCustomSource = auto;
            }
            if (i==1)
            {
                var qTS = from tb in db.SACHes select tb.TenSach;
                if (qTS!=null)
                {
                    foreach (var item in qTS)
                    {
                        auto.Add(item.Trim());
                    }
                }
                txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
                txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtSearch.AutoCompleteCustomSource = auto;
            }
            if (i==2)
            {
                var qTG = from tb in db.TACGIAs select tb.TenTacGia;
                if (qTG != null)
                {
                    foreach (var item in qTG)
                    {
                        auto.Add(item.Trim());
                    }
                }
                txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
                txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtSearch.AutoCompleteCustomSource = auto;
            }
            if (i==3)
            {
                var qNXB = from tb in db.NHAXUATBANs select tb.TenNXB;
                if (qNXB!=null)
                {
                    foreach (var item in qNXB)
                    {
                        auto.Add(item.Trim());
                    }
                    txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
                    txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    txtSearch.AutoCompleteCustomSource = auto;
                }
            }
        }

        private void frmSearch_Load(object sender, EventArgs e)
        {
           
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
           if (rdbChuyenNganh.Checked==false && rdbTenTG.Checked==false 
               && rdbTS.Checked==false && rdbNXB.Checked==false)
           {
               MessageBox.Show("Bạn Chưa Chọn Loại Tìm Kiếm","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
               txtSearch.Text = "";
           }
           else
           {
                if (rdbChuyenNganh.Checked == true)
            {
                var query = (from tbs in db.SACHes
                             join tbtl in db.THELOAIs on tbs.MaTheLoai equals tbtl.MaTheLoai
                             join tbtg in db.TACGIAs on tbs.TacGia equals tbtg.MaTacGia
                             join tbnxb in db.NHAXUATBANs on tbs.NhaXuatBan equals tbnxb.MaNXB
                             join tbtr in db.TINHTRANGs on tbs.MaTinhTrang equals tbtr.MaTinhTrang
                             where tbtl.TenTheLoai.Contains(txtSearch.Text)
                             select new
                             {
                                 tbs.MaSach,
                                 tbs.TenSach,
                                 tbtl.TenTheLoai,
                                 tbtg.TenTacGia,
                                 tbnxb.TenNXB,
                                 tbs.SoLuong,
                                 tbtr.TenTinhTrang
                             });
                dataGridViewX1.DataSource = query;
            }
            if (rdbTS.Checked==true)
            {
                var query = (from tbs in db.SACHes
                             join tbtl in db.THELOAIs on tbs.MaTheLoai equals tbtl.MaTheLoai
                             join tbtg in db.TACGIAs on tbs.TacGia equals tbtg.MaTacGia
                             join tbnxb in db.NHAXUATBANs on tbs.NhaXuatBan equals tbnxb.MaNXB
                             join tbtr in db.TINHTRANGs on tbs.MaTinhTrang equals tbtr.MaTinhTrang
                             where tbs.TenSach.Contains(txtSearch.Text)
                             select new
                             {
                                 tbs.MaSach,
                                 tbs.TenSach,
                                 tbtl.TenTheLoai,
                                 tbtg.TenTacGia,
                                 tbnxb.TenNXB,
                                 tbs.SoLuong,
                                 tbtr.TenTinhTrang
                             });
                dataGridViewX1.DataSource = query;
            }
            if (rdbTenTG.Checked==true)
            {
                var query = (from tbs in db.SACHes
                             join tbtl in db.THELOAIs on tbs.MaTheLoai equals tbtl.MaTheLoai
                             join tbtg in db.TACGIAs on tbs.TacGia equals tbtg.MaTacGia
                             join tbnxb in db.NHAXUATBANs on tbs.NhaXuatBan equals tbnxb.MaNXB
                             join tbtr in db.TINHTRANGs on tbs.MaTinhTrang equals tbtr.MaTinhTrang
                             where tbtg.TenTacGia.Contains(txtSearch.Text)
                             select new
                             {
                                 tbs.MaSach,
                                 tbs.TenSach,
                                 tbtl.TenTheLoai,
                                 tbtg.TenTacGia,
                                 tbnxb.TenNXB,
                                 tbs.SoLuong,
                                 tbtr.TenTinhTrang
                             });
                dataGridViewX1.DataSource = query;
            }
            if (rdbNXB.Checked==true)
            {
                var query = (from tbs in db.SACHes
                             join tbtl in db.THELOAIs on tbs.MaTheLoai equals tbtl.MaTheLoai
                             join tbtg in db.TACGIAs on tbs.TacGia equals tbtg.MaTacGia
                             join tbnxb in db.NHAXUATBANs on tbs.NhaXuatBan equals tbnxb.MaNXB
                             join tbtr in db.TINHTRANGs on tbs.MaTinhTrang equals tbtr.MaTinhTrang
                             where tbnxb.TenNXB.Contains(txtSearch.Text)
                             select new
                             {
                                 tbs.MaSach,
                                 tbs.TenSach,
                                 tbtl.TenTheLoai,
                                 tbtg.TenTacGia,
                                 tbnxb.TenNXB,
                                 tbs.SoLuong,
                                 tbtr.TenTinhTrang
                             });
                dataGridViewX1.DataSource = query;
            }
           }
        }

        private void rdbChuyenNganh_CheckedChanged(object sender, EventArgs e)
        {
            GetAutoComplete(0);
        }

        private void rdbTS_CheckedChanged(object sender, EventArgs e)
        {
            GetAutoComplete(1);
        }

        private void rdbTenTG_CheckedChanged(object sender, EventArgs e)
        {
            GetAutoComplete(2);
        }

        private void rdbNXB_CheckedChanged(object sender, EventArgs e)
        {
            GetAutoComplete(3);
        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
        }
    }
}
